package com.example.zaq.rest;

//20210520  zaq  账簿模块后端接口

import com.example.zaq.dao.*;
import com.example.zaq.model.*;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 阿恩
 * @date 2021/5/2017:06
 * 20210520  zaq  新增账簿查询以及账簿种类方法
 * 20210521  zaq  调试获取账簿类型、新建、删除、修改
 */
@RestController
@RequestMapping("/book")
public class AspBookRest {
    @Autowired
    SqlSessionFactory factory;

    /**
     * 获得月总支出汇总
     *http://localhost:9090/book/selectAllSummary_output
     * @return
     */
    @ResponseBody
    @PostMapping(value = "selectAllSummary_output",produces = "application/json;charset=UTF-8")
    public List<Summary> selectAllSummary_output(@RequestBody AspStatement body){
        System.out.println("获得值:"+body);
        List<AspStatement> aspBook=null;
        List<Summary> summaries=new List<Summary>() {
            @Override
            public int size() {
                return 0;
            }

            @Override
            public boolean isEmpty() {
                return false;
            }

            @Override
            public boolean contains(Object o) {
                return false;
            }

            @Override
            public Iterator<Summary> iterator() {
                return null;
            }

            @Override
            public Object[] toArray() {
                return new Object[0];
            }

            @Override
            public <T> T[] toArray(T[] a) {
                return null;
            }

            @Override
            public boolean add(Summary summary) {
                return false;
            }

            @Override
            public boolean remove(Object o) {
                return false;
            }

            @Override
            public boolean containsAll(Collection<?> c) {
                return false;
            }

            @Override
            public boolean addAll(Collection<? extends Summary> c) {
                return false;
            }

            @Override
            public boolean addAll(int index, Collection<? extends Summary> c) {
                return false;
            }

            @Override
            public boolean removeAll(Collection<?> c) {
                return false;
            }

            @Override
            public boolean retainAll(Collection<?> c) {
                return false;
            }

            @Override
            public void clear() {

            }

            @Override
            public Summary get(int index) {
                return null;
            }

            @Override
            public Summary set(int index, Summary element) {
                return null;
            }

            @Override
            public void add(int index, Summary element) {

            }

            @Override
            public Summary remove(int index) {
                return null;
            }

            @Override
            public int indexOf(Object o) {
                return 0;
            }

            @Override
            public int lastIndexOf(Object o) {
                return 0;
            }

            @Override
            public ListIterator<Summary> listIterator() {
                return null;
            }

            @Override
            public ListIterator<Summary> listIterator(int index) {
                return null;
            }

            @Override
            public List<Summary> subList(int fromIndex, int toIndex) {
                return null;
            }
        };
        try{
            int i=0;
            SqlSession sess=factory.openSession();
            AspStatementDao aspBookDao=sess.getMapper(AspStatementDao.class);
            summaries=aspBookDao.selectAllSummary_output(body);
            sess.close();
            for(i=0;i<summaries.size();i++){
                Summary summary=new Summary();
                System.out.println("输出sum"+i+":"+summary);
            }
            System.out.println(summaries);
            System.out.println("获得月总收入汇总结束");
            return summaries;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return summaries;
        }
    }

    /**
     * 获得月总收入汇总
     *http://localhost:9090/book/selectAllSummary_input
     * @return
     */
    @ResponseBody
    @PostMapping(value = "selectAllSummary_input",produces = "application/json;charset=UTF-8")
    public List<Summary> selectAllSummary_input(@RequestBody AspStatement body){
        System.out.println("获得值:"+body);
        List<AspStatement> aspBook=null;
        List<Summary> summaries=new List<Summary>() {
            @Override
            public int size() {
                return 0;
            }

            @Override
            public boolean isEmpty() {
                return false;
            }

            @Override
            public boolean contains(Object o) {
                return false;
            }

            @Override
            public Iterator<Summary> iterator() {
                return null;
            }

            @Override
            public Object[] toArray() {
                return new Object[0];
            }

            @Override
            public <T> T[] toArray(T[] a) {
                return null;
            }

            @Override
            public boolean add(Summary summary) {
                return false;
            }

            @Override
            public boolean remove(Object o) {
                return false;
            }

            @Override
            public boolean containsAll(Collection<?> c) {
                return false;
            }

            @Override
            public boolean addAll(Collection<? extends Summary> c) {
                return false;
            }

            @Override
            public boolean addAll(int index, Collection<? extends Summary> c) {
                return false;
            }

            @Override
            public boolean removeAll(Collection<?> c) {
                return false;
            }

            @Override
            public boolean retainAll(Collection<?> c) {
                return false;
            }

            @Override
            public void clear() {

            }

            @Override
            public Summary get(int index) {
                return null;
            }

            @Override
            public Summary set(int index, Summary element) {
                return null;
            }

            @Override
            public void add(int index, Summary element) {

            }

            @Override
            public Summary remove(int index) {
                return null;
            }

            @Override
            public int indexOf(Object o) {
                return 0;
            }

            @Override
            public int lastIndexOf(Object o) {
                return 0;
            }

            @Override
            public ListIterator<Summary> listIterator() {
                return null;
            }

            @Override
            public ListIterator<Summary> listIterator(int index) {
                return null;
            }

            @Override
            public List<Summary> subList(int fromIndex, int toIndex) {
                return null;
            }
        };
        try{
            int i=0;
            SqlSession sess=factory.openSession();
            AspStatementDao aspBookDao=sess.getMapper(AspStatementDao.class);
            summaries=aspBookDao.selectAllSummary_input(body);
            sess.close();
            for(i=0;i<summaries.size();i++){
                Summary summary=new Summary();
                System.out.println("输出sum"+i+":"+summary);
            }
            System.out.println(summaries);
            System.out.println("获得月总收入汇总结束");
            return summaries;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return summaries;
        }
    }

    /**
     * 删除账单明细
     * http://localhost:9090/book/deleteStatement
     * @param body
     * @return
     */
    @PostMapping("deleteStatement")
    @ResponseBody
    public AspStatement deleteStatement(@RequestBody AspStatement body){
        AspStatement aspBook=null;
        try{
            System.out.println("获得值:"+body);

            int i=0;
            SqlSession sess=factory.openSession();
            AspStatementDao aspBookDao=sess.getMapper(AspStatementDao.class);
            aspBook=aspBookDao.delete(body);
            sess.close();
            System.out.println(aspBook);
            System.out.println("删除账单明细结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 修改账单明细
     * http://localhost:9090/book/updateStatement
     * @param body
     * @return
     */
    @PostMapping("updateStatement")
    @ResponseBody
    public AspStatement updateStatement(@RequestBody AspStatement body){
        AspStatement aspBook=null;
        try{
            System.out.println("获得值:"+body);

            int i=0;
            SqlSession sess=factory.openSession();
            AspStatementDao aspBookDao=sess.getMapper(AspStatementDao.class);
            aspBook=aspBookDao.update(body);
            sess.close();
            System.out.println(aspBook);
            System.out.println("修改明细结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 新增账单明细
     * http://localhost:9090/book/insertStatement
     * @param body
     * @return
     */
    @PostMapping("insertStatement")
    @ResponseBody
    public AspStatement insertStatement(@RequestBody AspStatement body){
        AspStatement aspBook=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspStatementDao aspBookDao=sess.getMapper(AspStatementDao.class);
            aspBook=aspBookDao.insert(body);
            sess.close();
            System.out.println(aspBook);
            System.out.println("新增明细结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 获得账单明细
     *http://localhost:9090/book/getStatement
     * @return
     */
    @PostMapping("getStatement")
    @ResponseBody
    public List<AspStatement> getStatement(@RequestBody AspStatement body){
        System.out.println("获得值:"+body);
        List<AspStatement> aspBook=null;
        try{
            int i=0;
            SqlSession sess=factory.openSession();
            AspStatementDao aspBookDao=sess.getMapper(AspStatementDao.class);
            aspBook=aspBookDao.selectAll(body);
            sess.close();
            for(i=0;i<aspBook.size();i++){
                AspStatement asp=aspBook.get(i);

                asp.setUpdate(false);
                aspBook.set(i,asp);
                System.out.println("输出"+i+":"+asp);
            }
            System.out.println(aspBook);
            System.out.println("获得账单明细结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 获得支付类型
     *http://localhost:9090/book/getKindKind
     * @return
     */
    @PostMapping("getKindKind")
    @ResponseBody
    public List<AspKindKind> getKindKind(@RequestBody AspKindKind body){
        System.out.println("获得值:"+body);
        List<AspKindKind> aspBook=null;
        try{
            int i=0;
            SqlSession sess=factory.openSession();
            AspKindKindDao aspBookDao=sess.getMapper(AspKindKindDao.class);
            aspBook=aspBookDao.select(body);
            sess.close();
            for(i=0;i<aspBook.size();i++){
                AspKindKind asp=aspBook.get(i);
                aspBook.set(i,asp);
                System.out.println("输出"+i+":"+asp);
            }
            System.out.println(aspBook);
            System.out.println("根据获得支付类型结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 获得支付方式
     *http://localhost:9090/book/getKindPay
     * @return
     */
    @PostMapping("getKindPay")
    @ResponseBody
    public List<AspKindPay> getKindPay(@RequestBody AspKindPay body){
        System.out.println("获得值:"+body);
        List<AspKindPay> aspBook=null;
        try{
            int i=0;
            SqlSession sess=factory.openSession();
            AspKindPayDao aspBookDao=sess.getMapper(AspKindPayDao.class);
            aspBook=aspBookDao.select(body);
            sess.close();
            for(i=0;i<aspBook.size();i++){
                AspKindPay asp=aspBook.get(i);
                asp.setUpdate(false);
                aspBook.set(i,asp);
                System.out.println("输出"+i+":"+asp);
            }
            System.out.println(aspBook);
            System.out.println("根据获得支付方式结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 删除分组
     * http://localhost:9090/book/deleteGroup
     * @param body
     * @return
     */
    @PostMapping("deleteGroup")
    @ResponseBody
    public AspGroup deleteGroup(@RequestBody AspGroup body){
        AspGroup aspBook=null;
        try{
            System.out.println("获得值:"+body);

            int i=0;
            SqlSession sess=factory.openSession();
            AspGroupDao aspBookDao=sess.getMapper(AspGroupDao.class);
            aspBook=aspBookDao.delete(body);
            sess.close();
            System.out.println(aspBook);
            System.out.println("删除分组结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 修改分组名称
     * http://localhost:9090/book/updateGroup
     * @param body
     * @return
     */
    @PostMapping("updateGroup")
    @ResponseBody
    public AspGroup updateGroup(@RequestBody AspGroup body){
        AspGroup aspBook=null;
        try{
            System.out.println("获得值:"+body);

            int i=0;
            SqlSession sess=factory.openSession();
            AspGroupDao aspBookDao=sess.getMapper(AspGroupDao.class);
            aspBook=aspBookDao.update(body);
            sess.close();
            System.out.println(aspBook);
            System.out.println("修改分组结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 获得用户分组
     *http://localhost:9090/book/getGroup
     * @return
     */
    @PostMapping("getGroup")
    @ResponseBody
    public List<AspGroup> getGroup(@RequestBody AspGroup body){
        System.out.println("获得值:"+body);
        List<AspGroup> aspBook=null;
        try{
            int i=0;
            SqlSession sess=factory.openSession();
            AspGroupDao aspBookDao=sess.getMapper(AspGroupDao.class);
            aspBook=aspBookDao.selectAll(body);
            sess.close();
            for(i=0;i<aspBook.size();i++){
                AspGroup asp=aspBook.get(i);
                asp.setSelect(false);
                asp.setUpdate(false);
                aspBook.set(i,asp);
                System.out.println("输出"+i+":"+asp);
            }
            System.out.println(aspBook);
            System.out.println("根据获得分组信息结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 新增分组
     * http://localhost:9090/book/insertGroup
     * @param body
     * @return
     */
    @PostMapping("insertGroup")
    @ResponseBody
    public AspGroup insertGroup(@RequestBody AspGroup body){
        AspGroup aspBook=null;
        try{
            System.out.println("获得值:"+body);

            int i=0;
            SqlSession sess=factory.openSession();
            AspGroupDao aspBookDao=sess.getMapper(AspGroupDao.class);
            aspBook=aspBookDao.insert(body);
            sess.close();
            System.out.println(aspBook);
            System.out.println("新增分组结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 查询账簿组别模板
     * http://localhost:9090/book/selectKindGroup
     * @param body
     * @return
     */
    @PostMapping("selectKindGroup")
    @ResponseBody
    public AspKindGroup selectKindGroup(@RequestBody AspBook body){
        AspKindGroup aspBook=null;
        try{
            System.out.println("获得值:"+body);

            int i=0;
            SqlSession sess=factory.openSession();
            AspKindGroupDao aspBookDao=sess.getMapper(AspKindGroupDao.class);
            aspBook=aspBookDao.selectKindGroup(body.getbKind());
            sess.close();
            System.out.println(aspBook);
            System.out.println("查询分组模板结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 删除账簿
     * http://localhost:9090/book/deleteBook
     * @param body
     * @return
     */
    @PostMapping("deleteBook")
    @ResponseBody
    public AspBook deleteBook(@RequestBody AspBook body){
        AspBook aspBook=null;
        try{
            System.out.println("获得值:"+body);

            int i=0;
            SqlSession sess=factory.openSession();
            AspBookDao aspBookDao=sess.getMapper(AspBookDao.class);
            aspBook=aspBookDao.deleteBook(body);
            sess.close();
            System.out.println(aspBook);
            System.out.println("删除账簿结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }


    /**
     * 新增账簿
     * http://localhost:9090/book/insertBook
     * @param body
     * @return
     */
    @PostMapping("insertBook")
    @ResponseBody
    public AspBook insertBook(@RequestBody AspBook body){
        AspBook aspBook=null;
        try{
            //获得当前日期（用户创建日期）
            long time=System.currentTimeMillis();
            Date date=new Date(time);
            String mat="yyyyMMdd";
            SimpleDateFormat format=new SimpleDateFormat(mat);
            String strDate=format.format(date);
            int createDate=Integer.parseInt(strDate);
            body.setbCreatedate(createDate);
            System.out.println("获得值:"+body);

            int i=0;
            SqlSession sess=factory.openSession();
            AspBookDao aspBookDao=sess.getMapper(AspBookDao.class);
            aspBook=aspBookDao.insertBook(body);
            sess.close();
            System.out.println(aspBook);
            System.out.println("新增账簿结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 查账账簿类型模板
     * http://localhost:9090/book/getKindBook
     * @param body
     * @return
     */
    @PostMapping("getKindBook")
    @ResponseBody
    public List<AspKindBook> getKind(@RequestBody AspKindBook body){
        List<AspKindBook> aspBook=null;
        try{
            int i=0;
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspKindBookDao aspBookDao=sess.getMapper(AspKindBookDao.class);
            aspBook=aspBookDao.getAll(body);
            sess.close();
            for(i=0;i<aspBook.size();i++){
                AspKindBook asp=aspBook.get(i);
                aspBook.set(i,asp);
                System.out.println("输出"+i+":"+asp);
            }
            System.out.println(aspBook);
            System.out.println("获得账簿种类结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 修改账簿名称
     * http://localhost:9090/book/updateBook
     * @param body
     * @return
     */
    @PostMapping("updateBook")
    @ResponseBody
    public AspBook updateBook(@RequestBody AspBook body){
        System.out.println("获得值:"+body);
        AspBook aspBook=null;
        try{
            int i=0;
            SqlSession sess=factory.openSession();
            AspBookDao aspBookDao=sess.getMapper(AspBookDao.class);
            aspBook=aspBookDao.updateBook(body);
            sess.close();
            System.out.println(aspBook);
            System.out.println("修改账簿信息结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }

    /**
     * 获得用户账簿
     *http://localhost:9090/book/getBook
     * @return
     */
    @PostMapping("getBook")
    @ResponseBody
    public List<AspBook> getBook(@RequestBody AspBook body){
        System.out.println("获得值:"+body.getuId());
        List<AspBook> aspBook=null;
        try{
            int i=0;
            SqlSession sess=factory.openSession();
            AspBookDao aspBookDao=sess.getMapper(AspBookDao.class);
            aspBook=aspBookDao.selectUserBook(body.getuId());
            sess.close();
            for(i=0;i<aspBook.size();i++){
                AspBook asp=aspBook.get(i);
                asp.setSelectDrawer(false);
                asp.setBookUpdate(false);
                aspBook.set(i,asp);
                System.out.println("输出"+i+":"+asp);
            }
            System.out.println(aspBook);
            System.out.println("根据主键获得账簿信息结束");
            return aspBook;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspBook;
        }
    }
}
